<?php
namespace app\index\controller;

use app\common\controller\IndexBaseController;

class IndexController extends IndexBaseController {

	public function index() {
		$this->assign("meta_title","龙信云商铺");
		return $this -> fetch();
	}
	public function getOpenId(){
		$redirect_url ='http://'.$_SERVER['HTTP_HOST']."/index/index/getOpenIdByCode";
		$baseUrl='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa8273be4e9212fe1&redirect_uri='.urlencode($redirect_url).'&response_type=code&scope=snsapi_userinfo&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
		Header("Location: $baseUrl");
		exit;
	}
	public function getOpenIdByCode(){
		$appid='wxa8273be4e9212fe1';//Config::get("appid");
		$appsecret='14aab827eb72e0f560b61936e85cd2ed';//Config::get("appsecret");
		$code = input("code");//获取code
		echo $code;
		$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
		$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
		$array = get_object_vars($jsondecode);//转换成数组
		var_dump($array);
		$openid = $array['openid'];//输出openid
//		$nickname=$array['nickname'];//昵称
//		$sex=$array['sex'];//昵称
//		$headimgurl=$array['headimgurl'];//微信头像
//		$city=$array['city'];//城市
//		$province=$array['province'];//省份
//		$country==$array['country'];//国家
		if($openid){
//			 Db::name('member')->update(
//                  [
//                      'openid'   => $openid,
//                      'id'       => input("uid")
//                  ]
//           );
		}
		exit;
	}
	/*
	 * 注册短信示例
	 */
	public function regist_code($mobile) {
		return $this -> get_code($mobile, 2);
	}
	
	/*
	 * 验证注册短信示例
	 * 1成功、-1失败
	 */
	public function check_code($mobile, $code) {
		return check_code($mobile, $code, 2);
	}

}
